<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>邮件发送</title>
<style>
label {
display: inline-block;
width: 150px;
text-align: right;
}
input:not([type=submit]), textarea {
width: 400px;
}
.hint {
font-size: 12px;
color: #999;
margin-left: 10px;
}
#recipients div {
margin-bottom: 10px;
}
</style>
</head>
<body>
<form action="sendmail.php" method="post" enctype="multipart/form-data" onsubmit="return validateForm()">
<div id="recipients">
<label for="to0">收件人邮箱：</label>
<input type="email" id="to0" name="to[]" required>
<span class="hint">输入收件人邮箱，多个邮箱用逗号隔开</span><br>
</div>

<button type="button" id="add-recipient">添加接收人</button><br><br>

<label for="subject">邮件主题：</label>
<input type="text" id="subject" name="subject" required>
<span class="hint">输入邮件主题</span><br><br>

<label for="message">邮件内容：</label>
<textarea id="message" name="message" required></textarea>
<span class="hint">输入邮件内容</span><br><br>

<label for="template">邮件模板：</label>
<select id="template" onchange="loadTemplate()">
<option value="" selected>无</option>
<option value="template1">模板1</option>
<option value="template2">模板2</option>
</select>
<br><br>

<label for="attachment">附件：</label>
<input type="file" id="attachment" name="attachment"><br>
<span class="hint">添加附件（可选）</span><br><br>

<label for="time">发送时间：</label>
<input type="datetime-local" id="time" name="time" required>
<span class="hint">输入发送时间</span><br><br>

<input type="submit" value="发送">
</form>

<script>
let recipientCount = 1;
let recipientsContainer = document.getElementById("recipients");

// 添加接收人
document.getElementById("add-recipient").addEventListener("click", function () {
let newRecipient = document.createElement("div");
newRecipient.innerHTML = `
<label for="to${recipientCount}">收件人邮箱：</label>
<input type="email" id="to${recipientCount}" name="to[]" required>
<span class="hint">输入收件人邮箱，多个邮箱用逗号隔开</span><br>
`;
recipientsContainer.appendChild(newRecipient);
recipientCount++;
});

// 加载邮件模板
function loadTemplate() {
let templateName = document.getElementById("template").value;
if (templateName == "") {
return;
}
let xhr = new XMLHttpRequest();
xhr.open("GET", `templates/${templateName}.html`);
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
document.getElementById("message").value = xhr.responseText;
}
}
xhr.send();
}

// 表单验证
function validateForm() {
let to = document.querySelectorAll("input[name='to[]']");
let subject = document.getElementById("subject").value;
let message = document.getElementById("message").value;
let time = document.getElementById("time").value;

// 验证收件人邮箱是否正确
for (let recipient of to) {
if (!recipient.checkValidity()) {
alert("收件人邮箱格式不正确！");
return false;
}
}

// 验证表单是否填写完整
if (!to[0].value || !subject || !message || !time) {
alert("请填写完整的信息！");
return false;
}

// 安全性处理
if (isBlacklist(to[0].value)) {
alert("该邮箱已被加入黑名单，无法发送邮件！");
return false;
}
if (!isWhitelist(to[0].value)) {
alert("该邮箱不在白名单内，无法发送邮件！");
return false;
}

return true;
}

// 黑名单
function isBlacklist(email) {
let blacklist = ["spam@example.com", "junk@mail.com"];
return blacklist.includes(email);
}

// 白名单
function isWhitelist(email) {
let whitelist = ["2577539099@qq.com", "1755332755@qq.com"];
return whitelist.includes(email);
}
</script>
</body>
</html>
